package com.yunfangdata.web.controller;


import com.yunfangdata.domain.Permission;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IPermissionService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequiresPermissions("Permission:index")
    @PermissionName("权限首页")
    @RequestMapping("")
    public String index(){
        return "permission/list";
    }

    @RequiresPermissions("Permission:list")
    @PermissionName("权限列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return permissionService.query(qo);
    }

    @RequiresPermissions("Permission:load")
    @PermissionName("权限加载")
    @RequestMapping("load")
    @ResponseBody
    public JsonResult load(){
        JsonResult result = new JsonResult();
        try {
            permissionService.load();
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("加载失败");
        }
        return result;
    }

    @RequiresPermissions("Permission:delete")
    @PermissionName("权限删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            permissionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;
    }

    //角色中权限分配 所有权限
    @RequestMapping("permissions")
    @ResponseBody
    public List<Permission> permissions(){
        return permissionService.permissions();
    }

    //角色中权限分配 自身权限
    @RequestMapping("permissionsByRoleId")
    @ResponseBody
    public List<Permission> permissionsByRoleId(Long id){
        return permissionService.permissionsByRoleId(id);
    }




}
